using HKB.Common;
using System;
using System.Data;
using System.Web;
namespace HKB.Web.Admin
{
	public class OnlineAdmin
	{
		public int AdminID = 0;
		public string AdminName = null;
		public string AdminPassword = null;
		public string AdminGroupNames = null;
		public DateTime LastLoginTime;
		public DateTime EndDate;
		public int Access = 0;
		public string LastLoginIP = null;
		public int LoginTimes = 0;
		public string Power = null;
		public string ProvinceArea = "";
		public string CityArea = "";
		public string ErrStr = null;
		public bool Logined = false;
		public string AcceptIP = null;
		public string Disable = null;
		private Conn _conn = null;
		public OnlineAdmin()
		{
			this._conn = new Conn();
		}
		public OnlineAdmin(Conn conn)
		{
			this._conn = conn;
		}
		public bool CheckAdminLogin()
		{
			string text = HttpContext.Current.Server.UrlDecode(Fetch.GetCookies("Howave_Administrator"));
			string text2 = HttpContext.Current.Server.UrlDecode(Fetch.GetCookies("Howave_AdminPassword"));
			bool result;
			if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text2))
			{
				this.AdminName = text;
				this.AdminPassword = text2;
				result = this.CreateLogin(1);
			}
			else
			{
				result = false;
			}
			return result;
		}
		public bool LoginAdmin(string name, string pwd)
		{
			bool result;
			if (string.IsNullOrEmpty(name))
			{
				this.ErrStr = Language.GetAdminMsg("Login/NameNull");
				result = false;
			}
			else
			{
				if (string.IsNullOrEmpty(pwd))
				{
					this.ErrStr = Language.GetAdminMsg("Login/PassWordNull");
					result = false;
				}
				else
				{
					this.AdminName = name;
					this.AdminPassword = pwd;
					if (this.CreateLogin(0))
					{
						if (this.EndDate < DateTime.Now && this.Access != 1)
						{
							this.ErrStr = Language.GetAdminMsg("Login/AdminNameTimeOut");
							this.RemoveLogin();
							result = false;
						}
						else
						{
							if (this.AcceptIP != "")
							{
								if (!this.AcceptIP.Contains(Fetch.UserIp))
								{
									this.ErrStr = Language.GetAdminMsg("Login/IPLocked");
									this.RemoveLogin();
									result = false;
									return result;
								}
							}
							if (this.Disable == "True")
							{
								this.ErrStr = Language.GetAdminMsg("Login/AdminNameLocked");
								this.RemoveLogin();
								result = false;
							}
							else
							{
								this.CreateLoginCookies();
								result = true;
							}
						}
					}
					else
					{
						result = false;
					}
				}
			}
			return result;
		}
		public bool LogoutAdmin()
		{
			this.RemoveLoginCookies();
			return true;
		}
		private bool CreateLogin(int v)
		{
			string commandText = "select * from HL_Admin Where UserName=@adminname";
			IDbDataParameter dbDataParameter = this._conn.CreateParameter("@adminname", DbType.String, this.AdminName);
			DataTable dataTable = this._conn.GetDataTable(commandText, new IDbDataParameter[]
			{
				dbDataParameter
			});
			bool result;
			if (dataTable.Rows.Count > 0)
			{
				DataRow dataRow = dataTable.Rows[0];
				SiteConfigBll siteConfigBll = new SiteConfigBll();
				if (siteConfigBll.GetSiteString("SiteConfigInfo/CityArea") != "0")
				{
					if (dataRow["CityArea"].ToString() == "")
					{
						this.ProvinceArea = dataRow["ProvinceArea"].ToString();
					}
					else
					{
						this.CityArea = dataRow["CityArea"].ToString();
					}
				}
				string text = dataRow["PassWord"].ToString();
				if (v == 0)
				{
					string text2 = dataRow["SecurityKey"].ToString();
					text2 = Text.Md5(this.AdminPassword.ToUpper() + text2, 32);
					if (!Text.CheckMd5Text(text2, text))
					{
						this.ErrStr = Language.GetAdminMsg("Login/PassWordErr");
						this._conn.CloseConn();
						result = false;
						return result;
					}
				}
				else
				{
					if (this.AdminPassword != text)
					{
						this.ErrStr = Language.GetAdminMsg("Login/PassWordErr");
						this._conn.CloseConn();
						result = false;
						return result;
					}
				}
				this.AdminID = Convert.ToInt32(dataRow["Admin_ID"]);
				this.AdminName = dataRow["UserName"].ToString();
				this.AdminPassword = text;
				this.AdminGroupNames = "";
				this.LastLoginTime = Convert.ToDateTime(dataRow["LastLogin"].ToString());
				this.Access = Convert.ToInt32(dataRow["Access"]);
				this.LastLoginIP = dataRow["LastLoginIP"].ToString();
				this.LoginTimes = Convert.ToInt32(dataRow["LoginTimes"]);
				this.EndDate = Convert.ToDateTime(dataRow["EndDate"]);
				this.Power = this.PowerFromat(dataRow["Power"].ToString());
				this.AcceptIP = dataRow["AcceptIP"].ToString();
				this.Disable = dataRow["Disable"].ToString();
				this.Logined = true;
				if (this.Access == 1)
				{
					string text3 = "SuperAdmin";
					this.Power = text3;
				}
				else
				{
					string text3 = this.Power;
					IDbDataParameter dbDataParameter2 = this._conn.CreateParameter("@adminid", DbType.Int32, this.AdminID);
					DataTable dataTable2 = this._conn.GetDataTable("Select HL_AdminGroup.Access, HL_AdminGroup.Power,HL_AdminGroup.AdminGroupName FROM HL_AdminGroup INNER JOIN HL_AdminUserGroup ON HL_AdminGroup.AdminGourpID = HL_AdminUserGroup.GroupID INNER JOIN HL_Admin ON HL_AdminUserGroup.AdminID = HL_Admin.Admin_ID Where HL_AdminUserGroup.adminid=@adminid", new IDbDataParameter[]
					{
						dbDataParameter2
					});
					if (dataTable2.Rows.Count > 0)
					{
						string text4 = "";
						char[] array = text3.ToCharArray();
						for (int i = 0; i < text3.Length; i++)
						{
							char c = '0';
							for (int j = 0; j < dataTable2.Rows.Count; j++)
							{
								char[] array2 = this.PowerFromat(dataTable2.Rows[j]["Power"].ToString()).ToCharArray();
								if (i == 0)
								{
									this.AdminGroupNames = this.AdminGroupNames + dataTable2.Rows[j]["AdminGroupName"].ToString() + "|";
								}
								if (array.Length != array2.Length)
								{
									this.ErrStr = Language.GetAdminMsg("Group/GroupPowerErr");
									this._conn.CloseConn();
									result = false;
									return result;
								}
								if (array2[i] == '0')
								{
									c = '0';
								}
								else
								{
									if (array2[i] == '1')
									{
										c = '1';
									}
									else
									{
										c = '2';
									}
								}
							}
							text4 += c;
						}
						text3 = text4;
					}
					this.Power = text3;
				}
				result = true;
			}
			else
			{
				this.ErrStr = Language.GetAdminMsg("Login/NameErr");
				this._conn.CloseConn();
				result = false;
			}
			return result;
		}
		private string PowerFromat(string power)
		{
			power = power.Replace("|", "");
			power = power.Replace(",", "");
			return power;
		}
		public bool LoginOut()
		{
			this.RemoveLogin();
			this.RemoveLoginCookies();
			return true;
		}
		public string GetAccess()
		{
			string adminMsg;
			if (this.Access == 1)
			{
				adminMsg = Language.GetAdminMsg("Group/Super");
			}
			else
			{
				adminMsg = Language.GetAdminMsg("Group/General");
			}
			return adminMsg;
		}
		public string GetAdminGroupName()
		{
			string text = "";
			string result;
			if (string.IsNullOrEmpty(this.AdminGroupNames))
			{
				this._conn.OpenConn();
				DataTable dataTable = this._conn.GetDataTable("select AdminGroupName from HL_AdminGroup inner join HL_AdminUserGroup on HL_AdminGroup.AdminGourpID=HL_AdminUserGroup.GroupID Where HL_AdminUserGroup.AdminID=" + this.AdminID);
				this._conn.CloseConn();
				if (dataTable.Rows.Count <= 0)
				{
					result = "未分配";
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						text = text + dataTable.Rows[i]["AdminGroupName"].ToString() + "、";
					}
					if (text != "")
					{
						text = text.Remove(text.LastIndexOf("、"));
					}
					this.AdminGroupNames = text;
					result = text;
				}
			}
			else
			{
				result = this.AdminGroupNames;
			}
			return result;
		}
		public void RemoveLogin()
		{
			this.AdminID = 0;
			this.AdminName = null;
			this.AdminPassword = null;
			this.AdminGroupNames = null;
			this.Access = 0;
			this.LastLoginIP = null;
			this.LoginTimes = 0;
			this.Power = null;
			this.ErrStr = null;
			this.Logined = false;
			this.AcceptIP = null;
			this.Disable = null;
		}
		private void CreateLoginCookies()
		{
			HttpContext.Current.Response.Cookies["Howave_Administrator"].Value = HttpContext.Current.Server.UrlEncode(this.AdminName);
			HttpContext.Current.Response.Cookies["Howave_AdminPassword"].Value = HttpContext.Current.Server.UrlEncode(this.AdminPassword);
			SiteConfigBll siteConfigBll = new SiteConfigBll();
			if (siteConfigBll.GetSiteString("SiteConfigInfo/CityArea") != "0")
			{
				if (this.CityArea != "")
				{
					HttpContext.Current.Response.Cookies["Howave_CityArea"].Value = HttpContext.Current.Server.UrlEncode(this.CityArea);
					HttpContext.Current.Response.Cookies["Howave_ProvinceArea"].Expires = DateTime.Now.AddDays(-1.0);
				}
				else
				{
					if (this.CityArea == "" && this.ProvinceArea != "")
					{
						HttpContext.Current.Response.Cookies["Howave_ProvinceArea"].Value = HttpContext.Current.Server.UrlEncode(this.ProvinceArea);
						HttpContext.Current.Response.Cookies["Howave_CityArea"].Expires = DateTime.Now.AddDays(-1.0);
					}
				}
			}
		}
		private void RemoveLoginCookies()
		{
			HttpContext.Current.Response.Cookies["Howave_Administrator"].Value = "";
			HttpContext.Current.Response.Cookies["Howave_AdminPassword"].Value = "";
		}
		public bool CheckAdminPower(int key)
		{
			bool result;
			if (this.Power == "0")
			{
				result = false;
			}
			else
			{
				if (this.Power == "SuperAdmin")
				{
					result = true;
				}
				else
				{
					char[] array = this.Power.ToCharArray();
					result = (array[key] == '1');
				}
			}
			return result;
		}
		public bool CheckSuperAdmin()
		{
			return this.Access == 1;
		}
	}
}
